package cn.ptu.pmaa.controller;

import cn.ptu.pmaa.entities.FeedBack;
import cn.ptu.pmaa.entities.RespBean;
import cn.ptu.pmaa.service.UserService;
import cn.ptu.pmaa.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    TokenUtil tokenUtil;
    @Autowired()
    UserService userService;

    @RequestMapping("/getuserinfo")
    public RespBean getUserInfo(HttpServletRequest request){

        String token = request.getHeader("token");
        if (token.length()>0){
            Map<String,String> tokenMap = tokenUtil.parseToken(token);
            String userId = tokenMap.get("userId");
            String role = tokenMap.get("role");
            Object info =  userService.getUserInfo(userId,role);
            HashMap<String,Object> data = new HashMap<>();
            data.put("userinfo",info);
            return new RespBean("200","成功",data);
        }else
            return new RespBean("401","请登录");
    }

    @RequestMapping("/feedback")
    public RespBean uploadBack(@RequestParam String fknr,HttpServletRequest httpServletRequest){
        String token =  httpServletRequest.getHeader("token");
        Map<String, String> map = tokenUtil.parseToken(token);
        int flag = userService.uploadBack(new FeedBack(0, fknr, new Date(), map.get("userId")));
        if (flag==1){
            return new RespBean("200","反馈成功");
        }
        return new RespBean("400","反馈失败");
    }
}
